package com.tenglong.teacher.ui.common.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.tenglong.teacher.R;


/**
 * Created by Kevin on 2017/12/23.
 */

public class TakePicLayout extends FrameLayout implements View.OnClickListener {
    TextView mTextView;
    public RoundImageView mPic;
    ImageView mClose;
    ImageView mAdd;
    int mPostion;
    private OnCloseClickListener mCloseClickListener;

    public TakePicLayout(Context context) {

        this(context,null);
    }

    public TakePicLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);

    }
    public void setOnCloseClickListner(OnCloseClickListener l){
        mCloseClickListener=l;
    }
    public void setPostion(int postion){
        mPostion=postion;
    }

    public void setmPic(Uri uri){
        mPic.setImageURI(uri);
        mClose.setVisibility(VISIBLE);
        mTextView.setVisibility(GONE);
        mAdd.setVisibility(GONE);
        mPic.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        mPic.setImageHasRoundRect(30);
        mPic.setImageRotate(true);
    }
    public void setClear(){
        mClose.setVisibility(GONE);
        mTextView.setVisibility(GONE);
        mAdd.setVisibility(GONE);

    }


    public void setTitle(String title){

        mTextView.setText(title);

    }
    private void init(Context context, AttributeSet attrs){

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TakePicLayout);
        String label = typedArray.getNonResourceString(R.styleable.TakePicLayout_text);
        if(typedArray.getBoolean(R.styleable.TakePicLayout_group,false)){
            LayoutInflater.from(context).inflate(R.layout.take_pic_layout_g, this);
        }else{
            LayoutInflater.from(context).inflate(R.layout.take_pic_layout, this);
        }

        mTextView =findViewById(R.id.label);
        mTextView.setText(label);
        mPic=findViewById(R.id.p2s);
        mAdd=findViewById(R.id.temp);
        mClose=findViewById(R.id.close);
        mClose.setOnClickListener(this);
        mPic.setImageHasRoundRect(0);
        mPic.setImageRotate(false);


    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.close){
            mPic.setImageResource(R.drawable.single_add);
            mTextView.setVisibility(VISIBLE);
            mAdd.setVisibility(VISIBLE);
            mClose.setVisibility(GONE);
            mPic.setImageHasRoundRect(0);
            mPic.setImageRotate(false);
            if(mCloseClickListener!=null){
                mCloseClickListener.OnCloseClick(mPostion);
            }
        }
    }
    public void clearPic(){
        mPic.setImageResource(R.drawable.single_add);
        mTextView.setVisibility(VISIBLE);
        mAdd.setVisibility(VISIBLE);
        mClose.setVisibility(GONE);
        mPic.setImageHasRoundRect(0);
        mPic.setImageRotate(false);
        mPic.setScaleType(ImageView.ScaleType.FIT_XY);
    }
    public interface OnCloseClickListener {
        void OnCloseClick(int pos);
    }
}
